const router = require('koa-router')()
const Single = require('../modules/SingleSchema')
const moment = require('moment')
const util = require('../utils/util.js')
const log4js = require('./../utils/log4j')
const transformExl = require('./../utils/upload')
const Formula = require('../modules/formula')
router.prefix('/single')

/** 查询 */
router.get('/list', async (ctx) => {
  const { userName, deptName } = ctx.request.query
  const { page, skipIndex } = util.pager(ctx.request.query)
  let params = {}
  if (userName) params.userName = userName
  if (deptName) params.deptName = deptName
  const query = Single.find(params)
  const list = await query.skip(skipIndex).limit(page.pageSize)
  const total = await Single.countDocuments(params)
  ctx.body = util.success(
    {
      page: {
        ...page,
        total,
      },
      list,
    },
    '获取成功'
  )
})
/** 查询全部 */
router.get('/allList', async (ctx) => {
  try {
    const res = await Single.find({})
    ctx.body = util.success(res, '请求成功')
  } catch (error) {
    ctx.body = util.fail('异常错误')
  }
})
/** 还单 */
router.post('/update', async (ctx) => {
  const { userName, deptName, deptCount, updateName, updateCount } =
    ctx.request.body
  try {
    let up = parseInt(deptCount) - parseInt(updateCount)
    await Single.findOneAndUpdate(
      { deptName: deptName, userName: userName },
      { deptCount: up }
    )
    await Single.create({
      userName: updateName,
      deptName: deptName,
      deptCount: updateCount,
    })
    log4js.fatal(`${userName}+${deptName}-${updateCount}给${updateName}`)
    ctx.body = util.success({}, '更新成功')
  } catch (error) {
    ctx.body = util.fail(`异常错误=>${error.stack}`)
  }
})
/** 删除 */
router.post('/delete', async (ctx) => {
  const { userName, deptName } = ctx.request.body
  try {
    await Single.findOneAndDelete({ deptName: deptName, userName: userName })
    log4js.fatal(`删除=>${userName}+${deptName}`)
    ctx.body = util.success({}, '删除成功')
  } catch (error) {
    ctx.body = util.fail(`异常错误=>${error.stack}`)
  }
})
/** 更改 */
router.post('/change', async (ctx) => {
  const { userName, deptName, deptCount } = ctx.request.body
  try {
    await Single.findOneAndUpdate(
      { userName: userName, deptName: deptName },
      { userName: userName, deptName: deptName, deptCount: deptCount }
    )
    log4js.fatal(`更新=>${userName}+${deptName}+${deptCount}`)
    ctx.body = util.success({}, '更新成功')
  } catch (error) {
    ctx.body = util.fail(`异常错误=>${error.stack}`)
  }
})
/** 文件上传并且导入 */
router.post('/upload', async (ctx) => {
  try {
    const file = ctx.request.files.file
    const date = moment().format('M/DD/YY')
    const res = await transformExl(date, file)
    await Single.deleteMany()
    await Single.create(res)
    ctx.body = util.success({}, '导出成功')
  } catch (error) {
    log4js.error(`导出错误=>${error}`)
  }
})
/** 加单 */
router.post('/createSingle', async (ctx) => {
  const { userName, deptName, deptCount } = ctx.request.body
  try {
    await Single.create({
      userName: userName,
      deptName: deptName,
      deptCount: deptCount,
    })
    log4js.fatal(`加单=>${userName}+${deptName}+${deptCount}`)
    ctx.body = util.success({}, '添加成功')
  } catch (error) {
    ctx.body = util.fail(`异常错误=>${error.stack}`)
  }
})
/** 店铺列表*/
router.get('/deptList', async (ctx) => {
  const { name, belong } = ctx.request.query
  const { page, skipIndex } = util.pager(ctx.request.query)
  let params = {}
  if (name) params.name = name
  if (belong) params.belong = belong
  const query = Formula.find(params)
  const list = await query.skip(skipIndex).limit(page.pageSize)
  const total = await Formula.countDocuments(params)
  ctx.body = util.success(
    {
      page: {
        ...page,
        total,
      },
      list,
    },
    '获取成功'
  )
})
/** 添加公式 */
router.post('/deptAdd', async (ctx) => {
  const { name, formula, path, state, connect, belong } = ctx.request.body
  try {
    const res = await Formula.findOne({ name: name })
    if (res) {
      log4js.error(`添加店铺=>已存在，请勿重复添加`)
      ctx.body = util.fail('已存在，请勿重复添加')
    } else {
      await Formula.create({ name, formula, path, state, connect, belong })
      ctx.body = util.success('添加成功')
    }
  } catch (error) {
    log4js.error(`添加店铺异常=>${error}`)
    ctx.body = util.fail('错误异常')
  }
})
/** 删除店铺公式 */
router.post('/deptDel', async (ctx) => {
  const { name } = ctx.request.body
  try {
    await Formula.findOneAndDelete({ name: name })
    log4js.info(`删除店铺成功=>${name}`)
    ctx.body = util.success('删除成功')
  } catch (error) {
    log4js.error(`删除店铺失败=>${error}`)
    ctx.body = util.fail('删除失败')
  }
})

/** 更新 */
router.post('/deptUpdate', async (ctx) => {
  const { name, formula, path, state, connect, belong } = ctx.request.body
  try {
    await Formula.findOneAndUpdate(
      { name: name },
      { name, formula, path, state, connect, belong }
    )
    log4js.info(`更新店铺成功=>${name}`)
    ctx.body = util.success('更新成功')
  } catch (error) {
    log4js.error(`更新店铺失败=>${error}`)
    ctx.body = util.fail('更新失败')
  }
})

module.exports = router
